<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page isELIgnored="false"%>
<jsp:include page="../frame/header.jsp"></jsp:include>

<title>编辑帖子信息</title>
<style>

#imgDiv div{
	margin-left:8px;
	margin-top:8px;
	position:relative;
}
.del-img{
	position:absolute;
	left:0px;
	top:0px;
	z-index:9999;
	cursor:pointer;
	display:none;
}
.gray {
	filter: grayscale(100%);
	-webkit-filter: grayscale(100%);
	-moz-filter: grayscale(100%);
	-ms-filter: grayscale(100%);
	-o-filter: grayscale(100%);
	filter: url(desaturate.svg#grayscale);
	filter:progid:DXImageTransform.Microsoft.BasicImage(grayscale=1);
	-webkit-filter: grayscale(1);
}
</style>
</head>

<body>
	<div class="container-fluid">
		<div class="row-fluid">
			<div>
				<ul class="breadcrumb">
					<li><a href="#">娱乐交友</a> <span class="divider">/</span></li>
					<li><a href="#">论坛主题管理</a> <span class="divider">/</span></li>
					<li><a href="#">帖子管理</a></li>
				</ul>
			</div>

			<div class="box">
				<div class="box-header well" data-original-title>
					<h2>
						<i class="icon-edit"></i> 编辑主题帖子
					</h2>
					<div class="box-icon">
						<a href="#" class="btn btn-setting btn-round"><i
							class="icon-cog"></i></a> <a href="#"
							class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a>
						<a href="#" class="btn btn-close btn-round"><i
							class="icon-remove"></i></a>
					</div>
				</div>
				<div class="box-content">
					<form id="myform" class="form-horizontal" method="post" action="<c:url value="/admin/forum/topic" />">
						<fieldset>
							<input type="hidden" name="topicId" value="${topic.topicId }">
							<div class="top-pills">
								<div class="tabbable">
									<!-- Only required for left/right tabs -->
									<ul class="nav nav-tabs">
										<li class="active"><a href="#tab1" data-toggle="tab">帖子信息</a></li>
										<li><a href="#tab3" data-toggle="tab">广告设置</a></li>
									</ul>
									<div class="tab-content">
										<div class="tab-pane active" id="tab1">
											<div class="control-group">
												<label class="control-label" for="subject">帖子标题</label>
												<div class="controls">
													<input class="input-xlarge focused" name="subject"
														id="subject"  type="text" value="${topic.subject }" check-type="required" required-message="请输入帖子标题">
												</div>
											</div>
											<div class="control-group">
												<label class="control-label" for="forumId">所属版块</label>
												<div class="controls">
													<select data-placeholder="全部" id="forums_0" name="forumId" data-rel="chosen">
														
													</select>
												</div>
											</div>
											<div class="control-group">
														<label class="control-label" for="author">作者</label>
														<div class="controls">
															<input class="input-xlarge focused" name="author"
																id="author" type="text" value="${topic.author }">
														</div>
													</div>
											
											<div class="control-group">
												<label class="control-label" for="replies">回复数</label>
												<div class="controls">
													<input class="input-xlarge focused" name="replies"
														id="replies"  type="text" value="${topic.replies }">
												</div>
											</div>
											<div class="control-group">
												<label class="control-label" for="views">浏览数</label>
												<div class="controls">
													<input class="input-xlarge focused" name="views"
														id="views"  type="text" value="${topic.views }">
												</div>
											</div>
											<div class="control-group">
												<label class="control-label" for="message">帖子内容</label>
												<div class="controls">
													<textarea id="description_mc" name="message" rows="18" check-type="required" required-message="请输入帖子内容"
														 style="height:400px;width: 90%">${topic.message }</textarea>
												</div>
											</div>
											<div class="control-group">
												<label class="control-label" for="fileInput">上传图片</label>
												<div class="controls">
													<input class="input-file uniform_on" name="file"
														id="userHeader" type="file"> <input type="button" class="btn btn-success"
														value="upload" onclick="return ajaxFileUpload();">
													<img id="loading" class="hide" src="<c:url value="/resources/img/ajax-loader-6.gif"/>">
												</div>
											</div>
											<div class="control-group">
												<input type="hidden" name="images" id="images" value="${topic.images }">	
												<label class="control-label" for="fileInput">&nbsp;</label>
												<div class="controls" id="imgDiv">
													
												</div>
											</div>
											
											<div class="control-group">
												<label class="control-label" for="commentBaseCount">评论基数</label>
												<div class="controls">
													<input class="input-xlarge" id="commentBaseCount"
														name="commentBaseCount" type="text"
														value="${topic.commentBaseCount }" check-type="number">
												</div>
											</div>
				
											<input name="commentCount" type="hidden"
												value="${topic.commentCount }"> <input name="praiseCount"
												type="hidden" value="${topic.praiseCount }">
				
											<div class="control-group">
												<label class="control-label" for="praiseBaseCount">点赞基数</label>
												<div class="controls">
													<input class="input-xlarge" id="praiseBaseCount"
														name="praiseBaseCount" type="text"
														value="${topic.praiseBaseCount }" check-type="number">
												</div>
											</div>
										</div>
										
										<div class="tab-pane" id="tab3">
											<div class="control-group" id="type">
												<label class="control-label" for="status">是否启用</label>
												<div class="controls">
													<label class="radio inline"> 
														<input type="radio" name="status" value="1" <c:if test="${topic.advert.status == 1 }">checked</c:if>>是
													</label> 
													<label class="radio inline"> 
														<input type="radio" name="status" value="0" <c:if test="${topic.advert.status == 0 }">checked</c:if>>否
													</label>
												</div>
											</div>
											<div class="control-group" id="type">
												<label class="control-label" for="type">类型</label>
												<div class="controls">
													<label class="radio inline"> 
														<input type="radio" name="type" value="1" <c:if test="${topic.advert.type == 1 }">checked</c:if>>开机广告
													</label> 
													<label class="radio inline"> 
														<input type="radio" name="type" value="2" <c:if test="${topic.advert.type == 2 }">checked</c:if>>首页广告
													</label>
													<label class="radio inline"> 
														<input type="radio" name="type" value="0" <c:if test="${topic.advert.type == 0 }">checked</c:if>>列表页广告
													</label> 
												</div>
											</div>
											<div class="control-group" id="type">
												<label class="control-label" for="adPos">显示位置</label>
												<div class="controls">
													<label class="radio inline"> 
														<input type="radio" name="adPos" value="1" <c:if test="${topic.advert.adPos == 1 }">checked</c:if>>全屏显示
													</label> 
													<label class="radio inline"> 
														<input type="radio" name="adPos" value="0" <c:if test="${topic.advert.adPos == 0 }">checked</c:if>>置顶显示
													</label>
												</div>
											</div>
											<div class="control-group" id="type">
												<label class="control-label" for="adType">广告形式</label>
												<div class="controls">
													<label class="radio inline"> 
														<input type="radio" name="adType" value="0" <c:if test="${topic.advert.adType == 0 }">checked</c:if>>图片
													</label> 
													<label class="radio inline"> 
														<input type="radio" name="adType" value="1" <c:if test="${topic.advert.adType == 1 }">checked</c:if>>动图
													</label>
													<label class="radio inline"> 
														<input type="radio" name="adType" value="2" <c:if test="${topic.advert.adType == 2 }">checked</c:if>>视频
													</label>
												</div>
											</div>
											<div class="control-group">
												<label class="control-label" for="adContent">广告上传</label>
												<div class="controls">
													<input class="input-file uniform_on" name="file"
														id="file_1" type="file"> <input type="button"
														value="upload" onclick="return ajaxFileUpload_1();">
												</div>
												
											</div>
												<div class="control-group">
												<div class="controls">
													<img id="loading_1" class="hide"
													src="<c:url value="/resources/img/ajax-loader-6.gif"/>">
												<div id="picture_1"><img 
													src='<c:url value="/resources/fileupload/${topic.advert.adContent }"/>'
													height="100"></div> <input type="hidden" name="adContent"
													id="adContent" value="${topic.advert.adContent }">
												</div>
											</div>
										</div>
									</div>
								</div>
							</div>
							
							<div class="form-actions">
								<input type="hidden" name="advertStr" id="advertStr">
								<button id="saveBtn" type="button" class="btn btn-primary">保存</button>
								<button id="cancelBtn" type="button" class="btn">返回</button>
							</div>
						</fieldset>
					</form>

				</div>
			</div>
			<!--/span-->

		</div>
	</div>
	<!--/row-->
</body>

<jsp:include page="../frame/scripts.jsp"></jsp:include>
<script type="text/javascript">

var images = new Array();

$(document).ready(function(){
	$('#cancelBtn').click(function(){
		window.location.href = '<c:url value="/admin/forum/topic" />';	
	})
	
	var imgs = "${topic.images}";
	if(imgs && "" != imgs){
		$.each(imgs.split(","),function(i,item){
			setImages(item);
		})
	}
	
	$("#myform").validation();
	
	//保存
	$('#saveBtn').click(function(){
		var value = images.join(",");
		$("input[name='images']").val(value);
		
		var info = new Object();
		info.status = $("input[name=status]:checked").val();
		info.type = $("input[name=type]:checked").val();
		info.adPos = $("input[name=adPos]:checked").val();
		info.adType = $("input[name=adType]:checked").val();
		info.adContent = $("input[name='adContent']").val();
		
		var str = JSON.stringify(info);
		$("#advertStr").val(str);
		
		$("#myform").submit();
	})
	
	//xheditor 文件上传
	$('#description_mc').xheditor({
		tools:'full',
		skin:'default',
		upMultiple:1,
		html5Upload:false,
		upImgUrl:'<c:url value="/xheditor/fileupload"/>',
		upImgExt:'jpg,png,jpeg,gif,bmp',
		onUpload:function(data){
			
		}
	})
	
	//初始化论坛版块
		$.ajax({
			url:'<c:url value="/admin/forum/tree" />',
			dataType:'json',
			success:function(data){
				//搜索表单初始化版块下拉框
				createTree(data,"forums");
				//设置选中
				$("#forums_0").val("${searchForm.forumId}");
				
				//批量操作初始化版块下拉框
				createTree(data,"forums2");
				
				//增加下拉框样式
				//$('[data-rel="chosen"],[rel="chosen"]').chosen();
			}
		})		
		
	
})

	function createTree(data,tagId){
		$.each(data,function(i,item){
			//生成节点
			createNode(item,tagId);
			if(item.children.length > 0){
				//生成子节点
				createTree(item.children,tagId);
			}
		})
	}
	
	function createNode(item,tagId){
		if(item.forumId != 2){
			var $parent_select = $("#"+tagId+"_"+item.parentId);
			var str = '';
			if(item.children.length > 0){
				str += '<optgroup id="'+tagId+'_'+item.forumId+'" label="'+item.name+'">';
				str += '</optgroup>';
			}else{
				if(item.type == 1){
					str += '<option value='+item.forumId+'>'+item.name+'</option>';
				}
				
			}
			
			$parent_select.append(str);
		}
		
	}

function setImages(item){
	var image = DOMAIN + '/resources/fileupload/'+item;
	var delImg = '<c:url value="/resources/img/del.png"/>';
	var img = '<div style="float:left;height:100px;" onmouseout="hideClose()" onmouseover="showClose(this)"><img class="del-img" onclick="delImg(this,\''+item+'\')" src="'+delImg+'"><img class="pic" src="'+image+'" height="100"></div>';
	$("#imgDiv").append(img).show();
	images.push(item);	
}


//上传图片
function ajaxFileUpload(){
	
	$("#loading").ajaxStart(function(){
		//开始上传文件时显示一个图片
		$(this).show();
	}).ajaxComplete(function(){
		//文件上传完成将图片隐藏起来
		$(this).hide();
	});
	
	$.ajaxFileUpload({
		url:'<c:url value="/fileupload"/>',//用于文件上传的服务器端请求地址
		secureuri:false,//一般设置为false
		fileElementId:'userHeader',//文件上传空间的id属性  <input type="file" id="file" name="file" />
		dataType: 'JSON',//返回值类型 一般设置为json(大写)
		success: function (data, status){
			setImages($(data).html());
		},
		error: function (data, status, e){
			alert(e);
		}	
	})
	
	return false;
}

function showClose(obj){
	$(".del-img").hide();
	$(".pic").removeClass("gray");
	$(obj).find(".pic").addClass("gray");
	$(obj).find(".del-img").show();
}

function delImg(obj,value){
	$(obj).parent().remove();
	var index = $.inArray(value,images);
	images.splice(index,1);
}

function hideClose(){
	$(".del-img").hide();
	$(".pic").removeClass("gray");
}


function insertUpload(msg){
	console.log(msg);
}

//广告上传
function ajaxFileUpload_1() {
	var file = $("#file_1").val();
	var fileType = file.substring(file.lastIndexOf(".")+1); 
	if(fileType == ''){
		alert('请选择广告！');
		return;
	}
	fileType = fileType.toLowerCase();
	
	var adType = $("input[name=adType]:checked").val();
	
	if(adType == 0){
		$("#picture_1").html("<img width=300>");	
		if(fileType != 'png' && fileType != 'jpg' && fileType != 'bmp'){
			alert('图片类型不正确，系统支持三种图片类型：png,jpg,bmp!');
			return;
		}
	}else if(adType == 1){
		$("#picture_1").html("<img width=300>");	
		if(fileType != 'gif'){
			alert('动图类型不正确，系统仅支持gif动图!');
			return;
		}
	}else if(adType == 2){
		$("#picture_1").html("<vedio width=300>");	
		if(fileType != '3gp' && fileType != 'mp4' && fileType != 'mov'){
			//视频
			alert('视频类型不正确，系统支持三种视频类型：3gp,mp4,mov!');
			return;
		}
	}

	$("#loading_1").ajaxStart(function() {
		//开始上传文件时显示一个图片
		$(this).show();
	}).ajaxComplete(function() {
		//文件上传完成将图片隐藏起来
		$(this).hide();
	});

	$.ajaxFileUpload({
		url : '<c:url value="/fileupload"/>',//用于文件上传的服务器端请求地址
		secureuri : false,//一般设置为false
		fileElementId : 'file_1',//文件上传空间的id属性  <input type="file" id="file" name="file" />
		dataType : 'JSON',//返回值类型 一般设置为json(大写)
		success : function(data, status) {
			var image = DOMAIN + '/resources/fileupload/' + $(data).html();
			$("#picture_1").children(0).attr('src', image).show();
			$("input[name='adContent']").val($(data).html());
		},
		error : function(data, status, e) {
			alert(e);
		}
	})

	return false;
}
</script>